
package com.crowdfunding.circle.suggest.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crowdfunding.circle.common.controller.BaseController;
import com.crowdfunding.circle.common.param.SessionUserInfo;
import com.crowdfunding.circle.sensitiveword.SensitiveWordUtils;
import com.crowdfunding.circle.suggest.entity.SuggestVO;
import com.crowdfunding.circle.suggest.param.SuggestParam;
import com.crowdfunding.circle.suggest.service.SuggestService;

/**
 * 用户建议Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class SuggestController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(SuggestController.class);

    @Autowired
    private SuggestService suggestService;

    private final static String LIST_PAGE = "/admin/suggest_list";

    @RequestMapping(value = "/uncheck/suggest/update", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String update(SuggestVO suggestVO) {

        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        if (suggestVO == null) {
            resultMap.put("result", false);
            resultMap.put("message", "缺少必填项！");
            return JSONObject.toJSONString(resultMap);
        }

        if (StringUtils.isBlank(suggestVO.getName())) {
            resultMap.put("result", false);
            resultMap.put("message", "请填写姓名!");
            return JSONObject.toJSONString(resultMap);
        }

        if (StringUtils.isBlank(suggestVO.getUserContact())) {
            resultMap.put("result", false);
            resultMap.put("message", "请填写联系方式！");
            return JSONObject.toJSONString(resultMap);
        }

        if (StringUtils.isBlank(suggestVO.getSuggestInfo())) {
            resultMap.put("result", false);
            resultMap.put("message", "请填写建议内容!");
            return JSONObject.toJSONString(resultMap);
        }
        
        if (SensitiveWordUtils.isContaintSensitiveWord(suggestVO.getSuggestInfo(), 1)) {
            resultMap.put("result", false);
            resultMap.put("message", "建议内容中包含敏感词汇" + SensitiveWordUtils.getSensitiveWord(suggestVO.getSuggestInfo(), 1) + "，请检查后再保存");
            return JSONObject.toJSONString(resultMap);
        }
        
        if (SensitiveWordUtils.isContaintSensitiveWord(suggestVO.getUserContact(), 1)) {
            resultMap.put("result", false);
            resultMap.put("message", "联系方式中包含敏感词汇" + SensitiveWordUtils.getSensitiveWord(suggestVO.getUserContact(), 1) + "，请检查后再保存");
            return JSONObject.toJSONString(resultMap);
        }
        
        if (SensitiveWordUtils.isContaintSensitiveWord(suggestVO.getName(), 1)) {
            resultMap.put("result", false);
            resultMap.put("message", "姓名中包含敏感词汇" + SensitiveWordUtils.getSensitiveWord(suggestVO.getName(), 1) + "，请检查后再保存");
            return JSONObject.toJSONString(resultMap);
        }

        try {
            Integer affectCount = 0;
            if (suggestVO != null && StringUtils.isNotBlank(suggestVO.getSuggestId())) {
                affectCount = suggestService.update(suggestVO);
            } else if (suggestVO != null) {
                suggestVO.setSuggestId(UUID.randomUUID().toString());
                suggestVO.setCreateUser(seUser == null ? null : seUser.getUserId());
                suggestVO.setDataStatus(0);
                affectCount = suggestService.insert(suggestVO);
            }
            if (affectCount != null && affectCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "新增或修改用户建议成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增或修改用户建议失败");
            }
        } catch (Exception e) {
            logger.error("新增或修改用户建议出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改用户建议出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }

    @RequestMapping(value = "/admin/suggest/list")
    public ModelAndView queryList(SuggestParam suggestParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<SuggestVO> dataList = suggestService.queryByPage(suggestParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", suggestParam);
        return new ModelAndView(LIST_PAGE, resultMap);
    }
}
